/**********************************************************************************************************
** 驱动文件：	ups-MGEGalaxy300.c
** 驱动类型：	ups
** 设备名称：	UPS监测
** 设备型号：	MGEGalaxy300
** 设备品牌：	施耐德
** 设备协议：	Modbus-RTU
** 驱动描述：	适用于施耐德MGE Galaxy 300系列UPS。
** 生成日期：	2019-10-17 20:44:57
**********************************************************************************************************/
#include "Includes.h"


/**********************************************************************************************************
* 定义设备数据点变量结构
**********************************************************************************************************/
typedef struct{
	Var UPSStatus;								//UPS工作状态
	Var LoadPowered;							//输出供电
	Var LoadOnInverter;							//逆变器输出
	Var InternalHardwareFault;					//内部硬件故障
	Var UPSOnBattery;							//电池放电
	Var BatteryLowWarn;							//电池低压告警
	Var CommunicationFault;						//通讯故障
	Var UPSOverload;							//UPS过载
	Var EmergencyStop;							//紧急停机
	Var UPSTemperatureAlarm;					//UPS温度报警
	Var BatteryFault;							//电池故障
	Var DeviceVentilationFault;					//设备通风故障
	Var LoadAlarmViolationFault;				//负载报警违规故障
	Var MaintenanceBypassSwitchStatus;			//维修旁路开关状态
	Var RectifierFault;							//整流器故障
	Var BatteryTestResult;						//电池测试结果
	Var BatteryTestInProgress;					//电池测试中
	Var BatteryHighTemperature;					//电池高温报警
	Var BatterySwitchStatus;					//电池开关状态
	Var MainACVoltageStatus;					//市电状态
	Var MainACFuseStatus;						//市电保险丝状态
	Var MainACFrequencyStatus;					//市电频率状态
	Var InternalBypassHardware;					//内部旁路硬件故障
	Var InverterBypassSyncStatus;				//逆变器和旁路同步状态
	Var OutputSwitchStatus;						//输出开关状态
	Var ChargerFault;							//充电器故障
	Var BatteryChargingStatus;					//电池充电状态
	Var InverterMajorFault;						//逆变器严重故障
	Var InverterOverloadAlarm;					//逆变器过载报警
	Var InverterThermalOverloadAlarm;			//逆变器热过载报警
	Var InverterLimitationStatus;				//逆变器限流故障
	Var UPSFuseFault;							//UPS保险故障
	Var OutputShortCircuitAlarm;				//输出短路报警
	Var MainsInputIA;							//市电输入A相电流
	Var MainsInputIB;							//市电输入B相电流
	Var MainsInputIC;							//市电输入C相电流
	Var BypassInputIA;							//旁路输入A相电流
	Var BypassInputIB;							//旁路输入B相电流
	Var BypassInputIC;							//旁路输入C相电流
	Var OutputIA;								//输出A相电流
	Var OutputIB;								//输出B相电流
	Var OutputIC;								//输出C相电流
	Var BatteryCurrent;							//电池电流
	Var BypassInputUA;							//旁路输入A相电压
	Var BypassInputUB;							//旁路输入B相电压
	Var BypassInputUC;							//旁路输入C相电压
	Var OutputUA;								//输出A相电压
	Var OutputUB;								//输出B相电压
	Var OutputUC;								//输出C相电压
	Var UPSTemperature;							//UPS温度
	Var BatteryVoltage;							//电池电压
	Var OutputPA;								//输出A相有功功率
	Var OutputPB;								//输出B相有功功率
	Var OutputPC;								//输出C相有功功率
	Var OutputSA;								//输出A相视在功率
	Var OutputSB;								//输出B相视在功率
	Var OutputSC;								//输出C相视在功率
	Var TotalOutputActivePower;					//输出总有功功率
	Var TotalOutputApparentPower;				//输出总视在功率
	Var TotalOutputPercentLoad;					//输出负载百分比
	Var OutputPowerFactor;						//输出功率因数
	Var MainsInputFrequency;					//市电输入频率
	Var BypassInputFrequency;					//旁路输入频率
	Var OutputFrequency;						//输出频率
	Var BatteryRemainingRuntime;				//电池剩余运行时间
	Var BatteryTemperature;						//电池温度
	Var BatteryRemainingCapacity;				//电池剩余容量
	Var MainsInputUA;							//市电输入A相电压
	Var MainsInputUB;							//市电输入B相电压
	Var MainsInputUC;							//市电输入C相电压
}Data_Struct;									//定义变量结构


/**********************************************************************************************************
* 设备数据点变量属性声明
**********************************************************************************************************/
const static Var_Property_Def var_attr[] = {
	{
		OCTSTR,
		0,
		READONLY,
		"UPSStatus",
		"UPS工作状态",
		"",
		"UPS工作状态：0-未知，1-正常，2-告警，3-严重报警",
		0,
		0,
		"报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"LoadPowered",
		"输出供电",
		"",
		"输出供电状态：0-未供电，1-已供电",
		0,
		0,
		"未供电",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"LoadOnInverter",
		"逆变器输出",
		"",
		"逆变器输出：0-未启用，1-已启用",
		0,
		0,
		"未启用",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"InternalHardwareFault",
		"内部硬件故障",
		"",
		"内部硬件故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"UPSOnBattery",
		"电池放电",
		"",
		"电池使用状态：0-未放电，1-放电",
		0,
		0,
		"放电",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BatteryLowWarn",
		"电池低压告警",
		"",
		"电池低压告警：0-未发生，1-低压报警",
		0,
		0,
		"低压报警",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"CommunicationFault",
		"通讯故障",
		"",
		"通讯故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"UPSOverload",
		"UPS过载",
		"",
		"UPS过载：0-未发生，1-过载报警",
		0,
		0,
		"过载报警",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"EmergencyStop",
		"紧急停机",
		"",
		"紧急停机：0-未发生，1-紧急停机",
		0,
		0,
		"紧急停机",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"UPSTemperatureAlarm",
		"UPS温度报警",
		"",
		"UPS温度报警：0-正常，1-报警",
		0,
		0,
		"报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BatteryFault",
		"电池故障",
		"",
		"电池故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"DeviceVentilationFault",
		"设备通风故障",
		"",
		"设备通风故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"LoadAlarmViolationFault",
		"负载报警违规故障",
		"",
		"负载报警违规故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"MaintenanceBypassSwitchStatus",
		"维修旁路开关状态",
		"",
		"维修旁路开关状态：0-断开，1-闭合",
		0,
		0,
		"闭合",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"RectifierFault",
		"整流器故障",
		"",
		"整流器故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BatteryTestResult",
		"电池测试结果",
		"",
		"电池测试结果：0-正常，1-失败",
		0,
		0,
		"失败",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BatteryTestInProgress",
		"电池测试中",
		"",
		"电池测试进行中：0-无，1-测试中",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BatteryHighTemperature",
		"电池高温报警",
		"",
		"电池高温：0-正常，1-报警",
		0,
		0,
		"报警",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BatterySwitchStatus",
		"电池开关状态",
		"",
		"电池开关状态：0-断开，1-闭合",
		0,
		0,
		"断开",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"MainACVoltageStatus",
		"市电状态",
		"",
		"市电状态：0-正常，1-异常",
		0,
		0,
		"异常",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"MainACFuseStatus",
		"市电保险丝状态",
		"",
		"市电保险丝状态：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"MainACFrequencyStatus",
		"市电频率状态",
		"",
		"市电频率状态：0-正常，1-异常",
		0,
		0,
		"异常",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"InternalBypassHardware",
		"内部旁路硬件故障",
		"",
		"内部旁路硬件故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"InverterBypassSyncStatus",
		"逆变器和旁路同步状态",
		"",
		"逆变器和旁路同步状态：0-同步，1-不同步",
		0,
		0,
		"报警",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"OutputSwitchStatus",
		"输出开关状态",
		"",
		"输出开关状态：0-断开，1-闭合",
		0,
		0,
		"断开",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"ChargerFault",
		"充电器故障",
		"",
		"充电器故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"BatteryChargingStatus",
		"电池充电状态",
		"",
		"电池充电状态：0-未充电，1-充电中",
		0,
		0,
		"",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"InverterMajorFault",
		"逆变器严重故障",
		"",
		"逆变器严重故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"InverterOverloadAlarm",
		"逆变器过载报警",
		"",
		"逆变器过载：0-正常，1-报警",
		0,
		0,
		"报警",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"InverterThermalOverloadAlarm",
		"逆变器热过载报警",
		"",
		"逆变器热过载报警：0-正常，1-报警",
		0,
		0,
		"报警",
		"",
		0,
		0
	},
	{
		OCTSTR,
		0,
		READONLY,
		"InverterLimitationStatus",
		"逆变器限流故障",
		"",
		"逆变器限流故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"UPSFuseFault",
		"UPS保险故障",
		"",
		"UPS保险故障：0-正常，1-故障",
		0,
		0,
		"故障",
		"",
		0.000,
		0.000
	},
	{
		OCTSTR,
		0,
		READONLY,
		"OutputShortCircuitAlarm",
		"输出短路报警",
		"",
		"输出短路报警：0-正常，1-报警",
		0,
		0,
		"报警",
		"",
		0.000,
		0.000
	},
	{
		FLOAT,
		0,
		READONLY,
		"MainsInputIA",
		"市电输入A相电流",
		"A",
		"市电输入A相电流",
		0,
		0,
		"市电输入A相电流过高",
		"市电输入A相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"MainsInputIB",
		"市电输入B相电流",
		"A",
		"市电输入B相电流",
		0,
		0,
		"市电输入B相电流过高",
		"市电输入B相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"MainsInputIC",
		"市电输入C相电流",
		"A",
		"市电输入C相电流",
		0,
		0,
		"市电输入C相电流过高",
		"市电输入C相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassInputIA",
		"旁路输入A相电流",
		"A",
		"旁路输入A相电流",
		0,
		0,
		"旁路输入A相电流过高",
		"旁路输入A相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassInputIB",
		"旁路输入B相电流",
		"A",
		"旁路输入B相电流",
		0,
		0,
		"旁路输入B相电流过高",
		"旁路输入B相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassInputIC",
		"旁路输入C相电流",
		"A",
		"旁路输入C相电流",
		0,
		0,
		"旁路输入C相电流过高",
		"旁路输入C相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputIA",
		"输出A相电流",
		"A",
		"输出A相电流",
		0,
		0,
		"输出A相电流过高",
		"输出A相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputIB",
		"输出B相电流",
		"A",
		"输出B相电流",
		0,
		0,
		"输出B相电流过高",
		"输出B相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputIC",
		"输出C相电流",
		"A",
		"输出C相电流",
		0,
		0,
		"输出C相电流过高",
		"输出C相电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BatteryCurrent",
		"电池电流",
		"A",
		"电池电流",
		0,
		0,
		"电池电流过高",
		"电池电流过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassInputUA",
		"旁路输入A相电压",
		"V",
		"旁路输入A相电压",
		0,
		0,
		"旁路输入A相电压过高",
		"旁路输入A相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassInputUB",
		"旁路输入B相电压",
		"V",
		"旁路输入B相电压",
		0,
		0,
		"旁路输入B相电压过高",
		"旁路输入B相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassInputUC",
		"旁路输入C相电压",
		"V",
		"旁路输入C相电压",
		0,
		0,
		"旁路输入C相电压过高",
		"旁路输入C相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputUA",
		"输出A相电压",
		"V",
		"输出A相电压",
		0,
		0,
		"输出A相电压过高",
		"输出A相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputUB",
		"输出B相电压",
		"V",
		"输出B相电压",
		0,
		0,
		"输出B相电压过高",
		"输出B相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputUC",
		"输出C相电压",
		"V",
		"输出C相电压",
		0,
		0,
		"输出C相电压过高",
		"输出C相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"UPSTemperature",
		"UPS温度",
		"℃",
		"UPS温度",
		0,
		0,
		"UPS温度过高",
		"UPS温度过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BatteryVoltage",
		"电池电压",
		"V",
		"电池电压",
		0,
		0,
		"电池电压过高",
		"电池电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputPA",
		"输出A相有功功率",
		"KW",
		"输出A相有功功率",
		0,
		0,
		"输出A相有功功率过高",
		"输出A相有功功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputPB",
		"输出B相有功功率",
		"KW",
		"输出B相有功功率",
		0,
		0,
		"输出B相有功功率过高",
		"输出B相有功功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputPC",
		"输出C相有功功率",
		"KW",
		"输出C相有功功率",
		0,
		0,
		"输出C相有功功率过高",
		"输出C相有功功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputSA",
		"输出A相视在功率",
		"KVA",
		"输出A相视在功率",
		0,
		0,
		"输出A相视在功率过高",
		"输出A相视在功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputSB",
		"输出B相视在功率",
		"KVA",
		"输出B相视在功率",
		0,
		0,
		"输出B相视在功率过高",
		"输出B相视在功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputSC",
		"输出C相视在功率",
		"KVA",
		"输出C相视在功率",
		0,
		0,
		"输出C相视在功率过高",
		"输出C相视在功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"TotalOutputActivePower",
		"输出总有功功率",
		"KW",
		"输出总有功功率",
		0,
		0,
		"输出总有功功率过高",
		"输出总有功功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"TotalOutputApparentPower",
		"输出总视在功率",
		"KVA",
		"输出总视在功率",
		0,
		0,
		"输出总视在功率过高",
		"输出总视在功率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"TotalOutputPercentLoad",
		"输出负载百分比",
		"%",
		"输出负载百分比",
		0,
		0,
		"输出负载百分比过高",
		"输出负载百分比过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputPowerFactor",
		"输出功率因数",
		"",
		"输出功率因数",
		0,
		0,
		"输出功率因数过高",
		"输出功率因数过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"MainsInputFrequency",
		"市电输入频率",
		"Hz",
		"市电输入频率",
		0,
		0,
		"市电输入频率过高",
		"市电输入频率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BypassInputFrequency",
		"旁路输入频率",
		"Hz",
		"旁路输入频率",
		0,
		0,
		"旁路输入频率过高",
		"旁路输入频率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"OutputFrequency",
		"输出频率",
		"Hz",
		"输出频率",
		0,
		0,
		"输出频率过高",
		"输出频率过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BatteryRemainingRuntime",
		"电池剩余运行时间",
		"Min",
		"电池剩余运行时间",
		0,
		0,
		"电池剩余运行时间过高",
		"电池剩余运行时间过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BatteryTemperature",
		"电池温度",
		"℃",
		"电池温度",
		0,
		0,
		"电池温度过高",
		"电池温度过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"BatteryRemainingCapacity",
		"电池剩余容量",
		"%",
		"电池剩余容量",
		0,
		0,
		"电池剩余容量过高",
		"电池剩余容量过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"MainsInputUA",
		"市电输入A相电压",
		"V",
		"市电输入A相电压",
		0,
		0,
		"市电输入A相电压过高",
		"市电输入A相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"MainsInputUB",
		"市电输入B相电压",
		"V",
		"市电输入B相电压",
		0,
		0,
		"市电输入B相电压过高",
		"市电输入B相电压过低",
		0,
		0
	},
	{
		FLOAT,
		0,
		READONLY,
		"MainsInputUC",
		"市电输入C相电压",
		"V",
		"市电输入C相电压",
		0,
		0,
		"市电输入C相电压过高",
		"市电输入C相电压过低",
		0,
		0
	}
};


/**********************************************************************************************************
* 函数名称： static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
* 函数说明： 生成获取传感器数据指令
* 输入参数： 指令索引，传感器地址，变量结构体指针,以及指令缓存区指针,指令长度
* 返回参数： 无
**********************************************************************************************************/
static void cmd_fun(short index, int addr, void *data, char *cmd, short *len)
{
	switch(index)
	{
		case 0:
			cmd[0] = addr;
			cmd[1] = 0x04;
			cmd[2] = 0x00;
			cmd[3] = 0x00;
			cmd[4] = 0x00;
			cmd[5] = 0x01;
			*len = CRC16_Padding(cmd, 6);
			break;		
		case 1:
			cmd[0] = addr;
			cmd[1] = 0x04;
			cmd[2] = 0x00;
			cmd[3] = 0x40;
			cmd[4] = 0x00;
			cmd[5] = 0x10;
			*len = CRC16_Padding(cmd, 6);
			break;
		case 2:
			cmd[0] = addr;
			cmd[1] = 0x04;
			cmd[2] = 0x01;
			cmd[3] = 0x00;
			cmd[4] = 0x00;
			cmd[5] = 0x0F;
			*len = CRC16_Padding(cmd, 6);			
			break;
		case 3:
			cmd[0] = addr;
			cmd[1] = 0x04;
			cmd[2] = 0x01;
			cmd[3] = 0x1E;
			cmd[4] = 0x00;
			cmd[5] = 0x1A;
			*len = CRC16_Padding(cmd, 6);
			break;			
		case 4:
			cmd[0] = addr;
			cmd[1] = 0x04;
			cmd[2] = 0x01;
			cmd[3] = 0x39;
			cmd[4] = 0x00;
			cmd[5] = 0x1A;
			*len = CRC16_Padding(cmd, 6);
			break;			
			
		default:
			break;
	}
}


/**********************************************************************************************************
* 函数名称： static int parse_fun(short index, int addr, void *data, char *buf, short len)
* 函数说明： 解析传感器响应的数据，并写入数据库
* 输入参数： 解析索引,传感器地址，变量结构体指针，以及响应数据缓存取指针，响应数据长度
* 返回参数： 返回解析结果，返回0成功，返回1失败
**********************************************************************************************************/
static int parse_fun(short index, int addr, void *data, char *buf, short len)
{
	union{
		short			dat16;
     	unsigned char  	dat8[2];
   }DAT16_8;						   //数据类型转换	
	Data_Struct	*d = (Data_Struct*)data;

	if(buf!=NULL && buf[0]==addr && len>0 )
	{	
		if(CRC16_Check(buf,len))
		{
			switch(index)
			{
				case 0:	
					if(buf[4]==0){
						d->UPSStatus.value.str = "未知";
						Set_Var_Status(&d->UPSStatus, Alarm, NULL);
					}
					else if(buf[4]==2){
						d->UPSStatus.value.str = "正常";
						Set_Var_Status(&d->UPSStatus, Normal, NULL);
					}
					else if(buf[4]==4){
						d->UPSStatus.value.str = "告警";
						Set_Var_Status(&d->UPSStatus, Alarm, NULL);
					}
					else if(buf[4]==8){
						d->UPSStatus.value.str = "严重报警";
						Set_Var_Status(&d->UPSStatus, Alarm, NULL);
					}
					break;
				case 1:
					d->LoadPowered.value.str							= buf[4]&0x01?"已供电":"未供电";
					Set_Var_Status(&d->LoadPowered,						  buf[4]&0x01?Normal:Alarm, NULL);
					d->LoadOnInverter.value.str							= buf[4]&0x02?"已启用":"未启用";
					Set_Var_Status(&d->LoadOnInverter,					  buf[4]&0x02?Normal:Alarm, NULL);
					d->InternalHardwareFault.value.str					= buf[4]&0x04?"故障":"正常";
					Set_Var_Status(&d->InternalHardwareFault,			  buf[4]&0x04?Alarm:Normal, NULL);
					d->UPSOnBattery.value.str							= buf[4]&0x10?"放电":"未放电";
					Set_Var_Status(&d->UPSOnBattery,					  buf[4]&0x10?Alarm:Normal, NULL);					
					d->BatteryLowWarn.value.str							= buf[4]&0x60?"低压报警":"未发生";
					Set_Var_Status(&d->BatteryLowWarn,					  buf[4]&0x60?Alarm:Normal, NULL);

					d->CommunicationFault.value.str						= buf[3]&0x02?"通讯故障":"正常";
					Set_Var_Status(&d->CommunicationFault,				  buf[3]&0x02?Alarm:Normal, NULL);						
					d->UPSOverload.value.str							= buf[3]&0x04?"过载报警":"未发生";
					Set_Var_Status(&d->UPSOverload,						  buf[3]&0x04?Alarm:Normal, NULL);					
					d->EmergencyStop.value.str							= buf[3]&0x08?"紧急停机":"未发生";
					Set_Var_Status(&d->EmergencyStop,					  buf[3]&0x08?Alarm:Normal, NULL);	
					d->UPSTemperatureAlarm.value.str					= buf[3]&0x10?"故障":"正常";
					Set_Var_Status(&d->UPSTemperatureAlarm,				  buf[3]&0x10?Alarm:Normal, NULL);					
					d->BatteryFault.value.str							= buf[3]&0x20?"故障":"正常";
					Set_Var_Status(&d->BatteryFault,					  buf[3]&0x20?Alarm:Normal, NULL);
					d->DeviceVentilationFault.value.str					= buf[3]&0x40?"故障":"正常";
					Set_Var_Status(&d->DeviceVentilationFault,			  buf[3]&0x40?Alarm:Normal, NULL);					
					d->LoadAlarmViolationFault.value.str				= buf[3]&0x80?"故障":"正常";
					Set_Var_Status(&d->LoadAlarmViolationFault,			  buf[3]&0x80?Alarm:Normal, NULL);

					d->MaintenanceBypassSwitchStatus.value.str			= buf[6]&0x40?"闭合":"断开";
					Set_Var_Status(&d->MaintenanceBypassSwitchStatus,	  buf[6]&0x40?Alarm:Normal, NULL);
					d->RectifierFault.value.str							= buf[5]&0x02?"故障":"正常";
					Set_Var_Status(&d->RectifierFault,					  buf[5]&0x02?Alarm:Normal, NULL);
					
					d->BatteryTestResult.value.str						= buf[8]&0x04?"失败":"正常";
					Set_Var_Status(&d->BatteryTestResult,				  buf[8]&0x04?Alarm:Normal, NULL);
					d->BatteryTestInProgress.value.str					= buf[7]&0x01?"测试中":"无";
//					Set_Var_Status(&d->BatteryTestInProgress,			  buf[7]&0x01?ON:OFF, NULL);
					d->BatteryHighTemperature.value.str					= buf[7]&0x04?"报警":"正常";
					Set_Var_Status(&d->BatteryHighTemperature,			  buf[7]&0x04?Alarm:Normal, NULL);
					d->BatterySwitchStatus.value.str					= buf[7]&0x80?"闭合":"断开";
					Set_Var_Status(&d->BatterySwitchStatus,				  buf[7]&0x80?Normal:Alarm, NULL);
					
					d->MainACVoltageStatus.value.str					= buf[11]&0x01?"异常":"正常";
					Set_Var_Status(&d->MainACVoltageStatus,				  buf[11]&0x01?Alarm:Normal, NULL);
					d->MainACFuseStatus.value.str						= buf[11]&0x02?"故障":"正常";
					Set_Var_Status(&d->MainACFuseStatus,				  buf[11]&0x02?Alarm:Normal, NULL);
					d->MainACFrequencyStatus.value.str					= buf[11]&0x08?"异常":"正常";
					Set_Var_Status(&d->MainACFrequencyStatus,			  buf[11]&0x08?Alarm:Normal, NULL);


					d->InternalBypassHardware.value.str					= buf[18]&0x01?"故障":"正常";
					Set_Var_Status(&d->InternalBypassHardware,			  buf[18]&0x01?Alarm:Normal, NULL);
					d->InverterBypassSyncStatus.value.str				= buf[18]&0x02?"不同步":"同步";
					Set_Var_Status(&d->InverterBypassSyncStatus,		  buf[18]&0x02?Alarm:Normal, NULL);
					d->OutputSwitchStatus.value.str						= buf[17]&0x08?"闭合":"断开";
					Set_Var_Status(&d->OutputSwitchStatus,				  buf[17]&0x08?Normal:Alarm, NULL);
					
					d->ChargerFault.value.str							= buf[22]&0x01?"故障":"正常";
					Set_Var_Status(&d->ChargerFault,					  buf[22]&0x01?Alarm:Normal, NULL);
					d->BatteryChargingStatus.value.str					= buf[22]&0x08?"充电中":"未充电";
//					Set_Var_Status(&d->BatteryChargingStatus,			  buf[22]&0x08?ON:OFF, NULL);
					
					d->InverterMajorFault.value.str						= buf[28]&0x02?"故障":"正常";
					Set_Var_Status(&d->InverterMajorFault,				  buf[28]&0x02?Alarm:Normal, NULL);
					d->InverterOverloadAlarm.value.str					= buf[28]&0x04?"报警":"正常";
					Set_Var_Status(&d->InverterOverloadAlarm,			  buf[28]&0x04?Alarm:Normal, NULL);
					d->InverterThermalOverloadAlarm.value.str			= buf[28]&0x08?"报警":"正常";
					Set_Var_Status(&d->InverterThermalOverloadAlarm,	  buf[28]&0x08?Alarm:Normal, NULL);
					d->InverterLimitationStatus.value.str				= buf[28]&0x10?"报警":"正常";
					Set_Var_Status(&d->InverterLimitationStatus,		  buf[28]&0x10?Alarm:Normal, NULL);
					d->UPSFuseFault.value.str							= buf[28]&0x20?"报警":"正常";
					Set_Var_Status(&d->UPSFuseFault,					  buf[28]&0x20?Alarm:Normal, NULL);

					d->OutputShortCircuitAlarm.value.str				= buf[30]&0x02?"报警":"正常";
					Set_Var_Status(&d->OutputShortCircuitAlarm,			  buf[30]&0x02?Alarm:Normal, NULL);
					break;
				case 2:
					DAT16_8.dat8[1] = buf[3];
					DAT16_8.dat8[0] = buf[4];
					d->MainsInputIA.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[5];
					DAT16_8.dat8[0] = buf[6];
					d->MainsInputIB.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[7];
					DAT16_8.dat8[0] = buf[8];
					d->MainsInputIC.value.fnum = DAT16_8.dat16*1.0;
					
					DAT16_8.dat8[1] = buf[15];
					DAT16_8.dat8[0] = buf[16];
					d->BypassInputIA.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[17];
					DAT16_8.dat8[0] = buf[18];
					d->BypassInputIB.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[19];
					DAT16_8.dat8[0] = buf[20];
					d->BypassInputIC.value.fnum = DAT16_8.dat16*1.0;
					
					DAT16_8.dat8[1] = buf[21];
					DAT16_8.dat8[0] = buf[22];
					d->OutputIA.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[23];
					DAT16_8.dat8[0] = buf[24];
					d->OutputIB.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[25];
					DAT16_8.dat8[0] = buf[26];
					d->OutputIC.value.fnum = DAT16_8.dat16*1.0;

					DAT16_8.dat8[1] = buf[31];
					DAT16_8.dat8[0] = buf[32];
					d->BatteryCurrent.value.fnum = DAT16_8.dat16*1.0;
					break;
				case 3:					
					DAT16_8.dat8[1] = buf[3];
					DAT16_8.dat8[0] = buf[4];
					d->BypassInputUA.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[5];
					DAT16_8.dat8[0] = buf[6];
					d->BypassInputUB.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[7];
					DAT16_8.dat8[0] = buf[8];
					d->BypassInputUC.value.fnum = DAT16_8.dat16*1.0;
					
					DAT16_8.dat8[1] = buf[15];
					DAT16_8.dat8[0] = buf[16];
					d->OutputUA.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[17];
					DAT16_8.dat8[0] = buf[18];
					d->OutputUB.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[19];
					DAT16_8.dat8[0] = buf[20];
					d->OutputUC.value.fnum = DAT16_8.dat16*1.0;
					
					DAT16_8.dat8[1] = buf[31];
					DAT16_8.dat8[0] = buf[32];
					d->UPSTemperature.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[33];
					DAT16_8.dat8[0] = buf[34];
					d->BatteryVoltage.value.fnum = DAT16_8.dat16*1.0;
					
					DAT16_8.dat8[1] = buf[39];
					DAT16_8.dat8[0] = buf[40];
					d->OutputPA.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[41];
					DAT16_8.dat8[0] = buf[42];
					d->OutputPB.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[43];
					DAT16_8.dat8[0] = buf[44];
					d->OutputPC.value.fnum = DAT16_8.dat16*1.0;
					
					DAT16_8.dat8[1] = buf[45];
					DAT16_8.dat8[0] = buf[46];
					d->OutputSA.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[47];
					DAT16_8.dat8[0] = buf[48];
					d->OutputSB.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[49];
					DAT16_8.dat8[0] = buf[50];
					d->OutputSC.value.fnum = DAT16_8.dat16*1.0;
					
					DAT16_8.dat8[1] = buf[51];
					DAT16_8.dat8[0] = buf[52];
					d->TotalOutputActivePower.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[53];
					DAT16_8.dat8[0] = buf[54];
					d->TotalOutputApparentPower.value.fnum = DAT16_8.dat16*1.0;
					break;
				case 4:	
					DAT16_8.dat8[1] = buf[3];
					DAT16_8.dat8[0] = buf[4];
					d->TotalOutputPercentLoad.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[11];
					DAT16_8.dat8[0] = buf[12];
					d->OutputPowerFactor.value.fnum = DAT16_8.dat16*0.01;
					DAT16_8.dat8[1] = buf[13];
					DAT16_8.dat8[0] = buf[14];
					d->MainsInputFrequency.value.fnum = DAT16_8.dat16*0.1;
					DAT16_8.dat8[1] = buf[17];
					DAT16_8.dat8[0] = buf[18];
					d->BypassInputFrequency.value.fnum = DAT16_8.dat16*0.1;
					DAT16_8.dat8[1] = buf[19];
					DAT16_8.dat8[0] = buf[20];
					d->OutputFrequency.value.fnum = DAT16_8.dat16*0.1;
					DAT16_8.dat8[1] = buf[35];
					DAT16_8.dat8[0] = buf[36];
					d->BatteryRemainingRuntime.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[37];
					DAT16_8.dat8[0] = buf[38];
					d->BatteryTemperature.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[39];
					DAT16_8.dat8[0] = buf[40];
					d->BatteryRemainingCapacity.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[49];
					DAT16_8.dat8[0] = buf[50];
					d->MainsInputUA.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[51];
					DAT16_8.dat8[0] = buf[52];
					d->MainsInputUB.value.fnum = DAT16_8.dat16*1.0;
					DAT16_8.dat8[1] = buf[53];
					DAT16_8.dat8[0] = buf[54];
					d->MainsInputUC.value.fnum = DAT16_8.dat16*1.0;
					break;
				default:
					break;
			}
			return 0;
		}
	}
	return 1;
}


/**********************************************************************************************************
* 函数名称： void ups_MGEGalaxy300_Registration(void)
* 函数说明： UPS监测驱动注册
* 输入参数： 无
* 返回参数： 无
**********************************************************************************************************/
void ups_MGEGalaxy300_Registration(void)
{
	DeviceDriverRegistration(
		"ups",													//设备类型
		"UPS监测",												//设备名称（导航栏默认显示名称）
		"施耐德",												//设备品牌		
		"MGEGalaxy300",											//设备型号
		"适用于施耐德MGE Galaxy 300系列UPS。",					//驱动描述
		var_attr,												//变量属性声明
		sizeof(Data_Struct),									//变量结构体空间大小
		cmd_fun,												//发送指令生成函数
		parse_fun,												//数据解析函数
		500														//设备响应超时时间
	);
		
#if 0 || VirtualFrameEN
	#define CMD1 "\x01\x04\x00\x00\x00\x01\x31\xCA"
	#define RSP1 "\x01\x04\x02\x00\x04\xB8\xF3"	
	#define CMD2 "\x01\x04\x00\x40\x00\x10\xF0\x12"
	#define RSP2 "\x01\x04\x20\x40\x07\x01\x02\x80\x01\x00\x00\x00\x00\x00\x00\x40\x00\x08\x00\x00\x00\x00\x0A\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xD3\x4D"		
	#define CMD3 "\x01\x04\x01\x00\x00\x0C\xF1\xF3"
	#define RSP3 "\x01\x04\x1E\x00\x08\x00\x09\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x09\x00\x04\x00\x00\x00\x00\x00\x00\x0A\xFF"		
	#define CMD4 "\x01\x04\x01\x1E\x00\x1A\x10\x3B"
	#define RSP4 "\x01\x04\x34\x00\xEE\x00\xED\x00\xEE\x01\x9C\x01\x99\x01\x9D\x00\xE5\x00\xE5\x00\xE5\x01\x8D\x01\x8C\x01\x8D\x00\x00\x00\x00\x00\x00\x00\xDA\x00\x00\x00\x00\x00\x03\x00\x02\x00\x01\x00\x04\x00\x02\x00\x01\x00\x06\x00\x07\xA8\x0C"		
	#define CMD5 "\x01\x04\x01\x39\x00\x1A\xA0\x30"
	#define RSP5 "\x01\x04\x34\x00\x1C\x00\xA2\x00\x9C\x00\x00\x00\x5B\x01\xF3\x00\x00\x01\xF3\x01\xF3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x22\x00\x12\x00\x64\x00\x00\x00\x00\x00\x00\x00\x00\x00\xED\x00\xEC\x00\xEE\x2C\x37"		

	VirtualFrameRegistration(CMD1, sizeof(CMD1)-1, RSP1, sizeof(RSP1)-1);		
	VirtualFrameRegistration(CMD2, sizeof(CMD2)-1, RSP2, sizeof(RSP2)-1);		
	VirtualFrameRegistration(CMD3, sizeof(CMD3)-1, RSP3, sizeof(RSP3)-1);	
	VirtualFrameRegistration(CMD4, sizeof(CMD4)-1, RSP4, sizeof(RSP4)-1);
	VirtualFrameRegistration(CMD5, sizeof(CMD5)-1, RSP5, sizeof(RSP5)-1);		
#endif		
}

